// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MetricsAutomation.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Test.Robotics.Manipulation.Metrics
{
    using System;
    using System.Collections.Generic;
    
    using Microsoft.Robotics.IO;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Test.Robotics.Metrics;

    /// <summary>
    /// Hosts all the Manipulation metrics test cases, one per dataset/configuration
    /// </summary>
    [TestClass]
    public class MetricsAutomation
    {
        /// <summary>
        /// Stop on contact with blue ball during downward motion
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [Timeout(TestTimeout.Infinite)]
        [TestCategory("Metrics")]
        public void Manipulation_DownwardContactBlueBall()
        {
            // the name passed in must match the name of the row in SQL
            this.RunManipulationContactMetricsManifest(@"LBR4. Blue Ball, Down @ 0.1 m/s");
        }

        /// <summary>
        /// Stop on contact with rubber patch of Durometer 00-30 during downward motion
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [Timeout(TestTimeout.Infinite)]
        [TestCategory("Metrics")]
        public void Manipulation_DownwardContactOO30()
        {
            // the name passed in must match the name of the row in SQL
            this.RunManipulationContactMetricsManifest(@"LBR4. 00-30, Down @ 0.1 m/s");
        }

        /// <summary>
        /// Stop on contact with rubber patch of Durometer 00-30 during downward motion
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [Timeout(TestTimeout.Infinite)]
        [TestCategory("Metrics")]
        public void Manipulation_DownwardContactOO50()
        {
            // the name passed in must match the name of the row in SQL
            this.RunManipulationContactMetricsManifest(@"LBR4. 00-50, Down @ 0.1 m/s");
        }

        /// <summary>
        /// Stop on contact with rubber patch of Durometer 00-30 during downward motion
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [Timeout(TestTimeout.Infinite)]
        [TestCategory("Metrics")]
        public void Manipulation_DownwardContactOO70()
        {
            // the name passed in must match the name of the row in SQL
            this.RunManipulationContactMetricsManifest(@"LBR4. 00-70, Down @ 0.1 m/s");
        }

        /// <summary>
        /// Configures and starts the stop on contactact manifest.
        /// </summary>
        /// <param name="configurationName">The name of the configuration to load</param>
        private void RunManipulationContactMetricsManifest(string configurationName)
        {
            IEnumerable<Configuration> configSet = MetricsTestRunner.GetConfigurationsByName(configurationName);
            foreach (Configuration config in configSet)
            {
                LinearTouchConfiguration ltc = Serializer.Load<LinearTouchConfiguration>(config.ConfigurationData);

                Manifest m = ManipulationManifest.CreateLinearTouchMetricManifest(
                    ltc.StartPose,
                    ltc.EndPose,
                    config.IterationCount,
                    ltc.TargetPose,
                    ltc.LinearMaxSpeedInMetersPerSecond,
                    ltc.AngularMaxSpeedInRadiansPerSecond,
                    ltc.ForceThresholdInNewtons,
                    ltc.ForceAxesMask,
                    ltc.PositionAxesMask,
                    5000);

                MetricsTestRunner.Run(m, config, TimeSpan.FromHours(6), true);
            }
        }
    }
}
